


#Clear R
rm(list = ls())

#Set working directory
setwd("/Users/patrickhulme/Dropbox/Replicate/War_and_Responsibility")



#Here, we are taking the speeches and running them through sentimentR to get a dictionary-based prediction for support/ opposition to use of military force

#Load Data

rm(list = ls())

library(readr)
CSUMF_speeches <- read_csv("Data_files/CSUMF_speeches.csv") #raw speech data


#We are going to limit the analysis to speeches in crises related to the polls and votes we compare them to (Figures A8 & A9)

vote_data <- read_excel("Data_files/Votes_and_Polls/War_votes.xlsx")
crises_w_votes <- vote_data$crisno
crises_w_votes <- unique(crises_w_votes)

Polls <- read_csv("Data_files/polls_data.csv")
crises_w_polls <- Polls$crisno

#combine
crises_w_votes_or_polls <- c(crises_w_votes, crises_w_polls)
crises_w_votes_or_polls <- unique(crises_w_votes_or_polls)

dict_sentiment_speeches <- CSUMF_speeches[CSUMF_speeches$crisno %in% crises_w_votes_or_polls, ]

#####do simple dict method sentiment analysis:

#install.packages("sentimentr")
library(sentimentr)

#dict_sentiment_speeches$sent_score <- sentiment_by(dict_sentiment_speeches$speech)

library(magrittr)
library(dplyr)

out <- dict_sentiment_speeches %>%
  dplyr::mutate(dialogue_split = get_sentences(speech)) %$%
  sentiment_by(dialogue_split, list(crisno,MasterID))
#plot(out)

crisis_sent <- summaryBy(ave_sentiment ~ crisno, data=out, FUN=mean)


write.csv(crisis_sent,"/Data_files/Votes_and_Polls/dictionary_sent.csv", row.names = FALSE)
